package com.handinfo.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.engine.resource.SerializeableImage;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.Tools;

public class GameMacro
{
	public static final String MACRO_ICON = "";

	public GameMacro()
	{
	}

	public void initActions()
	{
		
	}

	public static void initStaticResource()
	{
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG, MACRO_ICON, 1);
		s_icon = ((SerializeableImage) ResourceManager.getInstance().loadResource(
				GameManager.getInstance().m_resource_static, head)).m_image;
	}

	public static void disposeStaticResource()
	{
		if (s_icon == null)
			return;
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG, MACRO_ICON, 1);
		ResourceManager.getInstance().removeResource(GameManager.getInstance().m_resource_static, head);
		s_icon = null;
	}

	/**
	 * 根据宏获得内部对象（技能，物品……）
	 * 
	 * @param marcos
	 * @return
	 */
	public static final Object[][] getObjectByMarco(GameMacro[] marcos)
	{
		Object[][] objects = new Object[marcos.length][];
		for (int i = 0; i < marcos.length; i++) {
			if (marcos[i] == null) {
				continue;
			}
			long[][] infos_marco = marcos[i].m_actionId;
			objects[i] = new Object[infos_marco.length];
			for (int j = 0; j < infos_marco.length; j++) {
				long type = infos_marco[j][MACRO_INDEX_TYPE];
				long id = infos_marco[j][MACRO_INDEX_ID];
				objects[i][j] = GameManager.getInstance().m_role.getDataByTypeAndId((int) type, id);
			}
		}
		return objects;
	}

	public static final int AMOUNT_MACRO_ACTION = 7;
	public static final int MACRO_INDEX_TYPE = 0;
	public static final int MACRO_INDEX_ID = 1;
	public static final int MACRO_INDEX_STATUS = 2;

	public static final int NUM_MARCO = 4;

	public static Image s_icon;
	public int m_id;
	public long[][] m_actionId = new long[AMOUNT_MACRO_ACTION][3];
	public int m_current_amount = 0;// 当前宏数量
	// public byte m_current_macro = 0;//当前宏
	public int m_current_action = 0;// 当前宏动作
	
	public void init() {
		m_current_action = 0;
	}

	public void reset() {
		if (m_actionId != null) {
			for (int i = 0; i < m_actionId.length; i++) {
				m_actionId[i][MACRO_INDEX_STATUS] = ActorRole.MACRO_STATE_READY;
			}
		}
	}
	// 初始化宏信息
	public void initActionId(int num)
	{
		m_actionId = new long[num][3];
		if (num != AMOUNT_MACRO_ACTION) {
			Tools.debugPrintln("-----------------------------");
			Tools.debugPrintln("当前数目与设定不等 num = " + num);
			Tools.debugPrintln("-----------------------------");
		}
	}
	public boolean checkMacroNull() {
		if (m_actionId != null) {
			for (int i = 0; i < m_actionId.length; i++) {
				if (m_actionId[i][1] >= CombatSkill.ID_DK_PG) {
					return false;
				}
			}
		}
		return true;
	}

	public void doAction()
	{
		int type = (int) m_actionId[m_current_action][0];
		// long id = m_actionId[m_current_action][1];
		switch (type) {
			case AbstractGameActor.ACTION_TYPE_NONE:
				m_current_action++;
				break;
			case AbstractGameActor.ACTION_TYPE_SKILL:
				int status = GameManager.getInstance().m_role.doAction(m_actionId[m_current_action]);
				m_actionId[m_current_action][MACRO_INDEX_STATUS] = status;
				if (status == ActorRole.MACRO_STATE_FINISH) {
					m_current_action++;
				}
				else if (status == ActorRole.MACRO_STATE_EXCEPTION) {
					m_current_action++;
				}
				break;
			case AbstractGameActor.ACTION_TYPE_ITEM:
				// GameManager.getInstance().m_role.getCombatSkill((short )
				// 0).m_iconId
				// GameManager.getInstance().m_role.getCombatSkill((short )
				// 0).m_iconCoverId
				// GameManager.getInstance().m_role.getCombatSkill((short )
				// 0).m_cd_counter == 0;
				break;
			case AbstractGameActor.ACTION_TYPE_FUNCTION:
				// GameManager.getInstance().m_role.getActorItemById(ActorItem.TYPE_BAG_ROLE,
				// id).m_iconId;
				// GameManager.getInstance().m_role.getActorItemById(ActorItem.TYPE_BAG_ROLE,
				// id).m_iconCoverId;
				break;
		}
		
		if (m_current_action >= AMOUNT_MACRO_ACTION) {
			m_current_action = 0;
			for (int i = 0; i < m_actionId.length; i++) {
				m_actionId[i][MACRO_INDEX_STATUS] = ActorRole.MACRO_STATE_READY;
			}
		}
		if (++m_timer > 100000) {
			m_timer = 0;
		}
		// do {
		// if (m_curAction >= m_actionNum) {
		// m_curAction = 0;
		// }
		// } while (GameScene.doAction(m_actionId[m_curAction++]) ==
		// MACRO_STATE_EXCEPTION);
	}
	private int m_timer = 0;
	public void drawIcon(Graphics g)
	{
		int gap = 2;
		int x = (AbstractGameScreen.SCREEN_WIDTH >> 1) - (GameScene.SIZE_IMG_ICON[0] + gap) * 3 - (GameScene.SIZE_IMG_ICON[0] + gap >> 1);
		int y = AbstractGameScreen.SCREEN_HEIGHT - (GameScene.SIZE_IMG_ICON[1] << 1) - 10;
		for (int i = 0; i < m_actionId.length; i++) {
			short id = (short) m_actionId[i][MACRO_INDEX_ID];
			byte status = (byte) m_actionId[i][MACRO_INDEX_STATUS];
			if (id > 0) {
				g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
				switch (status) {
					case ActorRole.MACRO_STATE_READY://蓝
						g.setColor(Tools.BLUE);
//						g.drawString("准", x + i * GameScene.SIZE_IMG_ICON[0], y, Tools.HCENTER_BOTTOM);
						break;
					case ActorRole.MACRO_STATE_GOING://绿
						int num = 2;
						if (m_timer % num == 0) {
							g.setColor(Tools.GREEN);
						} else {
							g.setColor(Tools.WHITE);
						}
//						g.drawString("进", x + i * GameScene.SIZE_IMG_ICON[0], y, Tools.HCENTER_BOTTOM);
						break;
					case ActorRole.MACRO_STATE_FINISH://白
						g.setColor(Tools.WHITE);
//						g.drawString("完", x + i * GameScene.SIZE_IMG_ICON[0], y, Tools.HCENTER_BOTTOM);
						break;
					case ActorRole.MACRO_STATE_EXCEPTION://红的
						g.setColor(Tools.RED);
//						g.drawString("异", x + i * GameScene.SIZE_IMG_ICON[0], y, Tools.HCENTER_BOTTOM);
						break;
					case ActorRole.MACRO_STATE_INTERUPT:
						g.setColor(Tools.WHITE);
//						g.drawString("断", x + i * GameScene.SIZE_IMG_ICON[0], y, Tools.HCENTER_BOTTOM);
						break;
				}
				RoleCombatSkill skill = GameManager.getInstance().m_role.getCombatSkill(id);
				if (skill != null) {
					if (status != ActorRole.MACRO_STATE_GOING) {
						g.fillRect(x + i * (GameScene.SIZE_IMG_ICON[0] + gap) - (gap >> 1), y - (gap >> 1),
								GameScene.SIZE_IMG_ICON[0] + gap, GameScene.SIZE_IMG_ICON[1] + gap);
					}
					g.setColor(Tools.BLACK);
					g.fillRect(x + i * (GameScene.SIZE_IMG_ICON[0] + gap), y, GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1]);
					skill.drawIcon(g, x + i * (GameScene.SIZE_IMG_ICON[0] + gap), y, Tools.LEFT_TOP);
				}
			} else {
				g.setColor(Tools.WHITE);
				g.drawRect(x + i * (GameScene.SIZE_IMG_ICON[0] + gap), y, GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1]);
			}
		}
		short id = (short) m_actionId[m_current_action][MACRO_INDEX_ID];
		if (id >= CombatSkill.ID_DK_PG) {
			drawImageCursor(g, x + m_current_action * (GameScene.SIZE_IMG_ICON[0] + gap) - 5, y - 5);
		}
	}
	private final int[] CURSOR_TRANS = { Tools.TRANS_NONE, Tools.TRANS_270, Tools.TRANS_HV, Tools.TRANS_90 };
	private void drawImageCursor(Graphics g, int x, int y)
	{
		g.setClip(x, y, GameScene.s_img_cursor_corner.getWidth(), GameScene.s_img_cursor_corner.getHeight());
		int timer = m_timer % 8;
		if (timer % 2 == 0) {
			Tools.drawRegion(g, GameScene.s_img_cursor_corner, x, y, CURSOR_TRANS[timer >> 1], Tools.LEFT_TOP);
		} else {
			Tools.drawRegion(g, GameScene.s_img_cursor_side, x, y, CURSOR_TRANS[timer >> 1], Tools.LEFT_TOP);
		}
	}
	
	public void drawMacroAction(Graphics g)
	{
		// for (int i = 0; i < m_actionNum; i++) {
		// int type = m_actionId[m_curAction] >> 28;
		// int id = m_actionId[m_curAction] & 0x0fffffff;
		// switch(id) {
		// case GameScene.ACTION_TYPE_SKILL:
		// break;
		// case GameScene.ACTION_TYPE_ITEM:
		// break;
		// case GameScene.ACTION_TYPE_FUNCTION:
		// break;
		// }
		// }
	}

	public void drawMacro(Graphics g, int x, int y)
	{
		drawMacro(g, x, y, -1);
	}

	/**
	 * 绘制宏标签 2012-4-5 上午10:59:08 venizeng
	 * @param g
	 * @param x
	 * @param y
	 */
	public void drawMacro(Graphics g, int x, int y, int index)
	{
		int iconId = 81;
		int w = GameScene.SIZE_IMG_ICON[0];
		int h = GameScene.SIZE_IMG_ICON[1];
		int colNum = GameScene.SIZE_IMG_ICON[2];
		AbstractGameScreen.drawRegion(g, GameScene.s_img_icon, (iconId % colNum) * w, (iconId / colNum) * h,
				w, h, 0, x, y, Tools.LEFT_TOP);
		//
		if (index == -1) {
			return;
		}
		Tools.drawNumber(g, GameScene.s_img_num, x + w, y + h, UITools.W_NUM,  UITools.H_NUM, index, 0, Tools.RIGHT_BOTTOM, false);
	}
}
